#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=10000000, mods=998244353;

void read(int n, int bx, int by, int cx, int cy, int p, int *b, int *c) {
  b[0]=by+1;c[0]=cy+1;
  for(int i=1;i<n;i++){
    b[i]=((long long)b[i-1]*bx+by)%p+1;
    c[i]=((long long)c[i-1]*cx+cy)%p+1;
  }
}

int max(int a, int b) {
  return a>b ? a : b;
}

long long qpower(long long a, int n) {
  long long s=1;
  for (; n>1; n/=2) {
    if (n%2) {
      s = s*a%mods;
    }
    a = a*a%mods;
  }
  return s*a%mods;
}

int main() {
  freopen("forging.in", "r", stdin);
  freopen("forging.out", "w", stdout);

  static long long f[maxn+1];
  static int b[maxn+1], c[maxn+1];
  int n, a, bx, by, cx, cy, p;
  scanf("%d %d", &n, &a);
  scanf("%d %d %d %d %d", &bx, &by, &cx, &cy, &p);
  read(n, bx, by, cx, cy, p, b, c);
  if (n==0) {
    printf("%d", a);
    return 0;
  } else {
    f[0] = a;
    for (int i=1; i<=n; i++) {
      f[i] = (f[max(i-1, 0)]+f[max(i-2, 0)])%mods;
    }
    printf("%lld", f[n]);
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
